from django.shortcuts import render
from django.http import HttpResponse
from .models import User
from django.core.paginator import Paginator, EmptyPage


# Create your views here.
def pages(request):
    return HttpResponse('pages')


def add(request):
    for i in range(100):
        User.objects.create(user_name='user' + '_' + str(i))
    return HttpResponse('数据创建成功！')


def show(request):
    page_num = request.GET.get('pageNum', 1)  # 获取前端用户访问的页码，默认设置为1
    page_data_num = request.GET.get('pageDataNum', 10)  # 获取每页显示的，数据个数，默认设置为10
    user_list = User.objects.all()  # 获取全都的数据
    page = Paginator(user_list, int(page_data_num))  # 创建分页器对象
    try:
        user_list = page.page(int(page_num))  # 显示当前页数的数据内容
    except EmptyPage:
        user_list = page.page(page.num_pages)  # 当页面超出列表，显示最后一页
    return render(request, 'myapp/index.html', locals())